#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "elf-parse.h"


int file_conv_buff(const char *filename, void **buffer)
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        return -1;
    }
    
    int filesize;
    fseek(fp, 0, SEEK_END);
    filesize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *buff = malloc(filesize);
    if (buff == NULL) {
        fclose(fp);
        return -1;
    }

    if (filesize != fread(buff, 1, filesize, fp)) {
        free(buff);
        fclose(fp);
        return -1;
    }
    
    *buffer = buff;
    fclose(fp);
    return 0;
}

int main(int argc, char *argv[])
{
    if (argc != 2 || argv[1] == NULL) {
        printf("invalid parameter, Format: %s <filename>\r\n", __FUNCTION__);
        return -1;
    }

    int ret;
    void *pbuffer;

    ret = file_conv_buff((char *)argv[1], &pbuffer);
    if (ret != 0) {
        return ret;
    }

    //! file parse
    elf_mem_loader(pbuffer);

    free(pbuffer);
    return 0;
}
